package com.threeti.mecool.web;

import java.lang.reflect.Field;
import java.util.Properties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.springframework.web.servlet.DispatcherServlet;

import com.threeti.mecool.core.domain.model.acl.PermissionConst;
import com.threeti.mecool.core.domain.model.plan.WorkFlowConst;

/**
 * 明酷Web端管理系统初始化 <p>
 * 
 * 1.预设权限相关常量，供Web页全局调用 
 * 2.预设工作流相关常量，供Web页全局调用
 * 
 * @author Jay Meng
 */
public class MeCoolFrontendInitializeServlet extends DispatcherServlet {

  @Override
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
    
    ServletContext appGlobalContext = config.getServletContext();
    String contentRepoPath =config.getInitParameter("content.repo.path");
    
    appGlobalContext.setAttribute("CONTENT_REPO_PATH", contentRepoPath);

    try {
      Class clazzObj = PermissionConst.class;
      Field[] fields = clazzObj.getDeclaredFields();
      for (Field field : fields) {// field.setAccessible(true);
        if (!"String[]".equals(field.getType().getSimpleName())) {
          appGlobalContext.setAttribute(field.getName(), field.get(clazzObj));
        } else {
          String[] strs = (String[]) field.get(clazzObj);
          appGlobalContext.setAttribute(field.getName() + "_CODE", strs[0]);
          appGlobalContext.setAttribute(field.getName() + "_TYPE", strs[1]);
        }
      }
      
      clazzObj = WorkFlowConst.class;
      fields = clazzObj.getDeclaredFields();
      for (Field field : fields) {// field.setAccessible(true);
        if (!"String[]".equals(field.getType().getSimpleName())) {
          appGlobalContext.setAttribute(field.getName(), field.get(clazzObj));
        }
      }
      
//      Enumeration<String> e = appGlobalContext.getAttributeNames();
//      while(e.hasMoreElements()) {
//        String name = e.nextElement();
//        System.out.println("name: " + name);
//        System.out.println("value: " + appGlobalContext.getAttribute(name));
//      }
    } catch (Exception e) {
      throw new ServletException("系统初始化失败", e);
    }
  }
}
